<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<style type="text/css">
	body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
	</style>
	<script type="text/javascript" src="https://api.map.baidu.com/api?type=webgl&v=1.0&ak={myak}"></script>
	<title>普通卫星地图模式</title>
</head>
<body>
	<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">

//从uri中取得 param_lat1 param_lon1 param_lat2 param_lon2 call mysign
const urlParams = new URLSearchParams(window.location.search);
const param_lat1 = urlParams.get('param_lat1');
const param_lon1 = urlParams.get('param_lon1');
const param_lat2 = urlParams.get('param_lat2');
const param_lon2 = urlParams.get('param_lon2');
var param_call = urlParams.get('param_call');
param_call = param_call.toUpperCase();  
var param_mysign = urlParams.get('param_mysign');
param_mysign = param_mysign.toUpperCase();  

  // 角度转弧度
function toRadians(degrees) {
    return degrees * Math.PI / 180;
}

// 弧度转角度
function toDegrees(radians) {
    return radians * 180 / Math.PI;
}

// 计算两个GPS坐标的中点
function calculateMidpoint(lat1, lon1, lat2, lon2) {
    // 转换为弧度
    const lat1Rad = toRadians(parseFloat(lat1));
    const lon1Rad = toRadians(parseFloat(lon1));
    const lat2Rad = toRadians(parseFloat(lat2));
    const lon2Rad = toRadians(parseFloat(lon2));
    
    // 计算球面中点
    const Bx = Math.cos(lat2Rad) * Math.cos(lon2Rad - lon1Rad);
    const By = Math.cos(lat2Rad) * Math.sin(lon2Rad - lon1Rad);
    
    const midLatRad = Math.atan2(
        Math.sin(lat1Rad) + Math.sin(lat2Rad),
        Math.sqrt((Math.cos(lat1Rad) + Bx) **2 + By** 2)
    );
    
    const midLonRad = lon1Rad + Math.atan2(By, Math.cos(lat1Rad) + Bx);
    
    // 转换为角度并返回（保留6位小数）
    return {
        latitude: toDegrees(midLatRad).toFixed(6),
        longitude: (toDegrees(midLonRad) + 360) % 360 // 确保经度在0-360范围内
    };
}
function calculateGPSDistance (lat1, lon1, lat2, lon2) {
  // 1. 转换坐标类型（字符串转数字）并转弧度
  const lat1Rad = toRadians (parseFloat (lat1));
  const lon1Rad = toRadians (parseFloat (lon1));
  const lat2Rad = toRadians (parseFloat (lat2));
  const lon2Rad = toRadians (parseFloat (lon2));
  // 2. 计算纬度差、经度差
  const deltaLat = lat2Rad - lat1Rad; // 纬度差值（弧度）
  const deltaLon = lon2Rad - lon1Rad; // 经度差值（弧度）
  // 3. Haversine 公式核心计算
  const a =
  Math.sin (deltaLat / 2) ** 2 + //sin²(Δφ/2)
  Math.cos (lat1Rad) * Math.cos (lat2Rad) * //cosφ1・cosφ2
  Math.sin (deltaLon / 2) ** 2; //sin²(Δλ/2)
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); // 2·atan2(√a, √(1−a))
  // 4. 计算距离（地球平均半径取 6371 公里，适用于大多数场景）
  const earthRadiusKm = 6371; // 地球平均半径（单位：公里）
  const distanceKm = earthRadiusKm * c;
  // 5. 保留 2 位小数返回（避免数值过长）
  return parseFloat (distanceKm.toFixed (2));
}
  var cs_lat = param_lat2 || "44.795076";
  var cs_lon = param_lon2 || "10.311334";
  var cs_sign=param_call || "dl8da";
  var cs_mysign=param_mysign || "bg8glr";
  var cs_mylat = param_lat1 || "29.496700";
  var cs_mylon = param_lon1 || "106.468400";

  // 计算中点
  var midpoint1 = calculateMidpoint(cs_lat, cs_lon, cs_mylat, cs_mylon);

	var map = new BMapGL.Map("allmap");    // 创建Map实例
	map.centerAndZoom(new BMapGL.Point(midpoint1.longitude,midpoint1.latitude), 1);  // 初始化地图,设置中心点坐标和地图级别
	map.enableScrollWheelZoom(true); 
	map.setMapType(BMAP_SATELLITE_MAP);      // 设置地图类型为普通卫星地图模式
  
var point1 = new BMapGL.Point(cs_mylon,cs_mylat);   
var marker1 = new BMapGL.Marker(point1);        // 创建标注   
map.addOverlay(marker1);                     // 将标注添加到地图中
  
 var point2 = new BMapGL.Point(cs_lon,cs_lat);   
var marker2 = new BMapGL.Marker(point2);        // 创建标注   
map.addOverlay(marker2);                     // 将标注添加到地图中

  var distance2 = calculateGPSDistance (cs_lat, cs_lon, cs_mylat, cs_mylon);
  
var label = new BMapGL.Label(cs_mysign+' '+distance2+'km', {       // 创建文本标注
    position: point1,                          // 设置标注的地理位置
    offset: new BMapGL.Size(3, 3)           // 设置标注的偏移量
})  
map.addOverlay(label);  
 label = new BMapGL.Label(cs_sign, {       // 创建文本标注
    position: point2,                          // 设置标注的地理位置
    offset: new BMapGL.Size(3, 3)           // 设置标注的偏移量
})  
map.addOverlay(label);   
  
  
//   var latlng = new BMapGL.Point( midpoint1.longitude,midpoint1.latitude);
// label = new BMapGL.Label(distance2+'km', {       // 创建文本标注
//     position: latlng
//   })  
              
            //创建地图文本对象
            // map.addOverlay(label); 

var polyline = new BMapGL.Polyline([point1,point2
	], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5});
map.addOverlay(polyline);
</script>
